#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > hfreqfromE.info <<'%EOF%'
   hfreqfromE
   ----------
   Molecule:         H2O
   Wave Function:    SCF / cc-pVDZ
   Test Purpose:     Harmonic frequencies calculated numerically from
                     energies
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > hfreqfromE.mol <<'%EOF%'
BASIS
cc-pVDZ
H2O

    2    0
        8.    1 
O      0.0000000000            0.0000000000           -0.7289126837
        1.    2
H      0.0000000000            1.4150113018            0.3644563419
H      0.0000000000           -1.4150113018            0.3644563419
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > hfreqfromE.dal <<'%EOF%'
**DALTON INPUT
.NMDDRV
**NMDDRV
.DORDR
  2  0
.SYMMETRY
  C2v
.NORMAL
**WAVE FUNCTIONS
.HF
*SCF INP
.THRESH
 1.0D-12
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >hfreqfromE.check
cat >>hfreqfromE.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

# Starting geometry and energy.
CRIT1=`$GREP "bond distance\: * H * O * (0| )\.946283" $log | wc -l`
CRIT2=`$GREP "Final * HF energy\: * \-76\.0270535127(2|3)" $log | wc -l`
CRIT3=`$GREP "Nuclear repulsion\: *    9\.30082413736" $log | wc -l`
CRIT4=`$GREP "Electronic energy\: * \-85\.32787765009" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[1]=11
ERROR[1]="STARTING GEOMETRY AND ENERGY NOT CORRECT"

# Matrix elements and character tables
CRIT1=`$GREP "1\. irep\:" $log | wc -l`
CRIT2=`$GREP "1\.00 * 1\.00 * 1\.00 * 1\.00" $log | wc -l`
CRIT3=`$GREP "2\. irep\:" $log | wc -l`
CRIT4=`$GREP "1\.00 \-1\.00 * 1\.00 \-1\.00" $log | wc -l`
CRIT5=`$GREP "3\. irep\:" $log | wc -l`
CRIT6=`$GREP "1\.00 * 1\.00 \-1\.00 \-1\.00" $log | wc -l`
CRIT7=`$GREP "4\. irep\:" $log | wc -l`
CRIT8=`$GREP "1\.00 \-1\.00 \-1\.00 * 1\.00" $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8`
CTRL[2]=12
ERROR[2]="SYMMETRY INPUT NOT CORRECT"

# Energies at the different geometries.
CRIT1=`$GREP "Final * HF energy\: * \-76\.0270264382[0-9][0-9]" $log | wc -l`
CRIT2=`$GREP "Final * HF energy\: * \-76\.0270267038[0-9][0-9]" $log | wc -l`
CRIT3=`$GREP "Final * HF energy\: * \-76\.0270305(499|500)[0-9][0-9]" $log | wc -l`
CRIT4=`$GREP "Final * HF energy\: * \-76\.027030240(2|3)[0-9][0-9]" $log | wc -l`
CRIT5=`$GREP "Final * HF energy\: * \-76\.0270401115[0-9][0-9]" $log | wc -l`
CRIT6=`$GREP "Final * HF energy\: * \-76\.0270399717[0-9][0-9]" $log | wc -l`
CRIT7=`$GREP "Final * HF energy\: * \-76\.0270535124[0-9][0-9]" $log | wc -l`
CRIT8=`$GREP "Final * HF energy\: * \-76\.0270535127[0-9][0-9]" $log | wc -l`
CRIT9=`$GREP "Final * HF energy\: * \-76\.0270535126[0-9][0-9]" $log | wc -l`
CRIT10=`$GREP "Final * HF energy\: * \-76\.0270142404[0-9][0-9]" $log | wc -l`
CRIT11=`$GREP "Final * HF energy\: * \-76\.0270338767[0-9][0-9]" $log | wc -l`
CRIT12=`$GREP "Final * HF energy\: * \-76\.0270417892[0-9][0-9]" $log | wc -l`
CRIT13=`$GREP "Final * HF energy\: * \-76\.0270368279[0-9][0-9]" $log | wc -l`
CRIT14=`$GREP "Final * HF energy\: * \-76\.0269688602[0-9][0-9]" $log | wc -l`
CRIT15=`$GREP "Final * HF energy\: * \-76\.026971315(4|5)[0-9][0-9]" $log | wc -l`
CRIT16=`$GREP "Final * HF energy\: * \-76\.027036787(2|3)[0-9][0-9]" $log | wc -l`
CRIT17=`$GREP "Final * HF energy\: * \-76\.0270511806[0-9][0-9]" $log | wc -l`
CRIT18=`$GREP "Final * HF energy\: * \-76\.0269745559[0-9][0-9]" $log | wc -l`
CRIT19=`$GREP "Final * HF energy\: * \-76\.0269754524[0-9][0-9]" $log | wc -l`
CRIT20=`$GREP "Final * HF energy\: * \-76\.027051222(1|2)[0-9][0-9]" $log | wc -l`
CRIT21=`$GREP "Final * HF energy\: * \-76\.026994125(1|2)[0-9][0-9]" $log | wc -l`
CRIT22=`$GREP "Final * HF energy\: * \-76\.0270404960[0-9][0-9]" $log | wc -l`
CRIT23=`$GREP "Final * HF energy\: * \-76\.0270405229[0-9][0-9]" $log | wc -l`
CRIT24=`$GREP "Final * HF energy\: * \-76\.026992540[5-7][0-9][0-9]" $log | wc -l`
CRIT25=`$GREP "Final * HF energy\: * \-76\.0270535127[0-9][0-9]" $log | wc -l`
CRIT26=`$GREP "Final * HF energy\: * \-76\.0270535088[0-9][0-9]" $log | wc -l`
CRIT27=`$GREP "Final * HF energy\: * \-76\.0270501437[0-9][0-9]" $log | wc -l`
CRIT28=`$GREP "Final * HF energy\: * \-76\.0269390616[0-9][0-9]" $log | wc -l`
CRIT29=`$GREP "Final * HF energy\: * \-76\.0270454323[0-9][0-9]" $log | wc -l`
CRIT30=`$GREP "Final * HF energy\: * \-76\.0269595888[0-9][0-9]" $log | wc -l`
CRIT31=`$GREP "Final * HF energy\: * \-76\.0269918020[0-9][0-9]" $log | wc -l`
CRIT32=`$GREP "Final * HF energy\: * \-76\.0270524980[0-9][0-9]" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12 \+ \
		$CRIT13 \+ $CRIT14 \+ $CRIT15 \+ $CRIT16 \+ $CRIT17 \+ $CRIT18 \+ \
		$CRIT19 \+ $CRIT20 \+ $CRIT21 \+ $CRIT22 \+ $CRIT23 \+ $CRIT24 \+ \
		$CRIT25 \+ $CRIT26 \+ $CRIT27 \+ $CRIT28 \+ $CRIT29 \+ $CRIT30 \+ \
		$CRIT31 \+ $CRIT32`
CTRL[3]=38
ERROR[3]="ENERGIES AT DIFFERENT GEOMETRIES NOT CORRECT"

# Hessian
CRIT1=`$GREP "1/ *1 * (0| )\.538[0-9][0-9][0-9]" $log | wc -l`
CRIT2=`$GREP "1/ *2 * (\-0| \-)\.333[0-9][0-9][0-9] * (0| )\.462[0-9][0-9][0-9]" $log | wc -l`
CRIT3=`$GREP "1/ *3 * (\-0| \-)\.380[0-9][0-9][0-9] * (0| )\.235[0-9][0-9][0-9] * (0| )\.269[0-9][0-9][0-9]" $log | wc -l`
CRIT4=`$GREP "2/ *4 * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9]" $log | wc -l`
CRIT5=`$GREP "2/ *5 * (\-0|0|\-| )\.000[0-9][0-9][0-9] * (\-0|0|\-| )\.000[0-9][0-9][0-9] * (\-0|0|\-| )\.000[0-9][0-9][0-9] * (\-0| \-)\.000[0-9][0-9][0-9] * (\-0|0|\-| )\.000[0-9][0-9][0-9]" $log | wc -l`
CRIT6=`$GREP "3/ *6 * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9]" $log | wc -l`
CRIT7=`$GREP "4/ *7 * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9]" $log | wc -l`
CRIT8=`$GREP "4/ *8 * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9]" $log | wc -l`
CRIT9=`$GREP "4/ *9 * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9] * (0| )\.000[0-9][0-9][0-9]" $log | wc -l`
CRIT10=`$GREP "4/ *7 * (0| )\.785[0-9][0-9][0-9]" $log | wc -l`
CRIT11=`$GREP "4/ *8 * (\-0| \-)\.555[0-9][0-9][0-9] * (0| )\.392[0-9][0-9][0-9]" $log | wc -l`
CRIT12=`$GREP "4/ *9 * (\-0| \-)\.429[0-9][0-9][0-9] * (0| )\.303[0-9][0-9][0-9] * (0| )\.234[0-9][0-9][0-9]" $log | wc -l`
TEST[4]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12`
CTRL[4]=12
ERROR[4]="HESSIAN NOT CORRECT"

# Frequencies and normal coordinates.
CRIT1=`$GREP "1 * 4113\.[0-9][0-9] * (0| )\.0187[0-9][0-9]" $log | wc -l`
CRIT2=`$GREP "1 * 1775\.[0-9][0-9] * (0| )\.0080[0-9][0-9]" $log | wc -l`
CRIT3=`$GREP "4 * 4212\.[0-9][0-9] * (0| )\.0191[0-9][0-9]" $log | wc -l`
CRIT4=`$GREP "(\-0|0|\-| )\.0000[0-9][0-9][0-9] * (\-0|0|\-| )\.0000[0-9][0-9][0-9] * (\-0|0|\-| )\.0000[0-9][0-9][0-9]" $log | wc -l`
CRIT5=`$GREP "(0| )\.0000[0-9][0-9][0-9] * (0| )\.0000[0-9][0-9][0-9] * (\-0| \-)\.0703[0-9][0-9][0-9]" $log | wc -l`
CRIT6=`$GREP "(\-0| \-)\.0505[0-9][0-9][0-9] * (\-0| \-)\.0701[0-9][0-9][0-9] * (0| )\.0000[0-9][0-9][0-9]" $log | wc -l`
CRIT7=`$GREP "(0| )\.5814[0-9][0-9][0-9] * (\-0| \-)\.4325[0-9][0-9][0-9] * (0| )\.5581[0-9][0-9][0-9]" $log | wc -l`
CRIT8=`$GREP "(0| )\.4008[0-9][0-9][0-9] * (0| )\.5571[0-9][0-9][0-9] * (0| )\.4312[0-9][0-9][0-9]" $log | wc -l`
CRIT9=`$GREP "(\-0| \-)\.5814[0-9][0-9][0-9] * (0| )\.4325[0-9][0-9][0-9] * (0| )\.5581[0-9][0-9][0-9]" $log | wc -l`
CRIT10=`$GREP "(0| )\.4008[0-9][0-9][0-9] * (0| )\.5571[0-9][0-9][0-9] * (\-0| \-)\.4312[0-9][0-9][0-9]" $log | wc -l`
CRIT11=`$GREP "Number of energy * calculations done\: * 33" $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11`
CTRL[5]=13
ERROR[5]="FREQUENCIES AND NORMAL COORDINATES NOT CORRECT"

PASSED=1
for i in 1 2 3 4 5
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
